/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.providers;

import com.googlecode.sca4idea.SCABundle;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.KeyEvent;
import java.awt.*;

public class ProviderConfigurationEditor extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JList providerList;
    private JButton copyButton;
    private JButton button2;
    private JTextField textField1;
    private JTree implementationTree;
    private JTextField implementationNameTextField;
    private JLabel implementationTypeLabel;
    private SCAProvider[] providers;

    public ProviderConfigurationEditor(Frame parent, SCAProvider selectedProvider) {
        super(parent, SCABundle.message("dialog.provider.configuration.editor.title"));

        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        providerList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent event) {
                onProviderSelected();
            }
        });
        implementationTree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                onImplementationSelected();
            }
        });

        // Build the list of providers
        DefaultListModel model = new DefaultListModel();
        providerList.setModel(model);
        providers = SCAProviderRegistry.getInstance().getAllProviders();
        for (SCAProvider provider : providers) {
            model.addElement(new SCAProviderListItem(provider));
            if (provider == selectedProvider) {
                providerList.setSelectedIndex(providerList.getModel().getSize() - 1);
            }
        }

        // Configure the implementation tree
        implementationTree.setShowsRootHandles(true);

        // Init the panel showing the implementation configuration
        implementationNameTextField.setEnabled(false);
    }

    private void onOK() {
        dispose();
    }

    private void onCancel() {
        dispose();
    }

    private void onProviderSelected() {
        SCAProvider provider = ((SCAProviderListItem) providerList.getSelectedValue()).getProvider();

        // Build the implementation tree, and ensure it is expanded
        MutableTreeNode root = new ImplementationsRootNode();
        implementationTree.setModel(new DefaultTreeModel(root));
        for (SCAImplementationProvider implProvider : provider.getImplementationProviders()) {
            root.insert(new ImplementationTypeNode(implProvider), root.getChildCount());
        }
        implementationTree.expandPath(new TreePath(root));

        // Select the first implementation
        if (root.getChildCount() > 0) {
            implementationTree.setSelectionPath(new TreePath(new Object[] { root, root.getChildAt(0) }));
        }
    }

    private void onImplementationSelected() {
        
    }

    private static class SCAProviderListItem {
        private final SCAProvider provider;

        private SCAProviderListItem(SCAProvider provider) {
            this.provider = provider;
        }

        public SCAProvider getProvider() {
            return provider;
        }

        public String toString() {
            return provider.getDisplayName();
        }
    }

    private static class ImplementationsRootNode extends DefaultMutableTreeNode {
        @Override
        public String toString() {
            return SCABundle.message("implementations.root");
        }
    }

    private static class ImplementationTypeNode extends DefaultMutableTreeNode {
        private final SCAImplementationProvider implProvider;

        private ImplementationTypeNode(SCAImplementationProvider implProvider) {
            this.implProvider = implProvider;
        }

        @Override
        public String toString() {
            return implProvider.getDisplayName();
        }
    }
}
